//
//  ZHLEEStatusCell.swift
//  ZHLEE_Blog_Swift_Pro
//
//  Created by ZHLEEBlander on 15/10/16.
//  Copyright © 2015年 ZHLEEBlander. All rights reserved.
//

import UIKit

class ZHLEEStatusCell: UITableViewCell {

// MARK: - 
// MARK: -----------------属性--------------------------
    var statusFrame: ZHLEEStatusFrame? {
    
        didSet{
        
            setCellView()
        }
    }
    //-----------------原创微博--------------------------
    ///  原创微博的整体View
    private lazy var originalView: UIView = {
    
        let view = UIView()
        
        view.backgroundColor = UIColor.whiteColor()
        
        //添加内容
        view.addSubview(self.headImageView)
        view.addSubview(self.nameLabel)
        view.addSubview(self.vipImageView)
        view.addSubview(self.createdTimeLabel)
        view.addSubview(self.sourceLabel)
        view.addSubview(self.originalTextLabel)
        view.addSubview(self.originalPhotos)
        
        return view
    }()
    /// 头像
    private lazy var headImageView : UIImageView = {
    
        let imageView = UIImageView()
        
        //设置边线
        imageView.layer.borderColor = UIColor.lightGrayColor().CGColor
        imageView.layer.borderWidth = 0.5
        imageView.layer.masksToBounds = true
        
        return imageView
    }()
    /// 名字
    private lazy var nameLabel : UILabel = {
        
        let label = UILabel()
        
        label.font = UIFont.systemFontOfSize(Cell_Status_Name_Font)
        
        return label
    }()
    /// 会员图标
    private lazy var vipImageView: UIImageView = UIImageView()
    
    /// 创建时间
    private lazy var createdTimeLabel: UILabel = {
    
        let label = UILabel()
        
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(Cell_Status_CreateTime_Font)
        return label
        
    }()
    /// 来源view
    private lazy var sourceLabel : UILabel = {
    
        let label = UILabel()
        label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(Cell_Status_Scource_Font)
        
        return label
    }()
    /// 原创内容view
    private lazy var originalTextLabel : UILabel = {
        
        let label = UILabel()
        
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(Cell_Status_Text_Font)
        
        return label
        }()
    
    /// 原创微博的配图
    private lazy var originalPhotos : ZHLEEStatusPhotos = ZHLEEStatusPhotos()
    
    //-----------------转发View--------------------------
    
    private lazy var retweetedView: UIView = {
    
        let view = UIView()
        
        view.backgroundColor = RGB(r: 244, g: 244, b: 244)
        
        //转发微博的内容
        view.addSubview(self.retweetedTextLabel)
        //转发微博的图片
        view.addSubview(self.retweetPhotos)
        
        return view
    }()
    /// 转发微博
    private lazy var retweetedTextLabel: UILabel = {
    
        let label = UILabel()
        
        label.numberOfLines = 0
        
        label.font = UIFont.systemFontOfSize(Cell_Status_Text_Font)
        
        return label
        
    }()
    /// 转发微博配图
    private lazy var retweetPhotos: ZHLEEStatusPhotos = ZHLEEStatusPhotos()
    
    //底部toolBar
    private lazy var cellToolBarView: ZHLEEStatusToolBarView = ZHLEEStatusToolBarView()
// MARK: - 
// MARK: -----------------方法--------------------------
    
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        contentView.backgroundColor = RGB(r: 240, g: 240, b: 240)
        //添加原创微博
        contentView.addSubview(originalView)
        
        //添加转发微博
        contentView.addSubview(retweetedView)
        
        //添加底部tool
        contentView.addSubview(cellToolBarView)
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setCellView(){
    
        let statusF = self.statusFrame!
        let status = statusF.status!
        
        //原创微博高度
        originalView.frame = statusF.originalViewF!
        
        //设置头像
        headImageView.frame = statusF.headImageF!
        headImageView.sd_setImageWithURL(NSURL(string: status.user!.profile_image_url!), placeholderImage: UIImage(named: "avatar_default_big"))
        
        //名称
        nameLabel.frame = statusF.nameLabelF!
        nameLabel.text = status.user?.name
        
        //会员
        if status.user!.isVip{
        
            vipImageView.hidden = false
            vipImageView.frame = statusF.vipImageF!
            vipImageView.image = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
            
            nameLabel.textColor = UIColor.orangeColor()
            
        }else{
        
            vipImageView.hidden = true
            nameLabel.textColor = UIColor.blackColor()
        }
        
        //创建时间
        let creatAt = status.createdAtStr
        createdTimeLabel.frame = statusF.creatTimeF!
        createdTimeLabel.width = creatAt.size(createdTimeLabel.font).width
        createdTimeLabel.text = creatAt
        
        //来源
        sourceLabel.frame = statusF.sourceLabelF!
        sourceLabel.text = status.source!
        
        //原创微博内容
        originalTextLabel.frame = statusF.contentTextF!
        originalTextLabel.text = status.text!
        
        //原创配图
        //判断,有没有配图
        if let originalPicUrls = status.pic_urls where originalPicUrls.count > 0{
            originalPhotos.hidden = false
            originalPhotos.frame = statusF.originalPhotosF!
            originalPhotos.pic_urls = originalPicUrls
        }else{
        
            originalPhotos.hidden = true
        }
        //转发微博-->判断
        if let reStatus = status.retweeted_status {
        
            retweetedView.hidden = false
            //转发微博view的frame
            retweetedView.frame = statusF.retweetedViewF!
            //转发微博 内容frame
            retweetedTextLabel.frame = statusF.retweetedTextF!
            
            //设置数据
            retweetedTextLabel.text = reStatus.text
            
            //判断有没有转发图片
            if let photoUrl = reStatus.pic_urls where photoUrl.count > 0{
            
                retweetPhotos.hidden = false
                retweetPhotos.frame = statusF.retweetPhotosF!
                retweetPhotos.pic_urls = photoUrl
            }else{
                retweetPhotos.hidden = true
            }
            
        }else {
        
            //没有转发微博
            retweetedView.hidden = true
        }
        //底部view
        cellToolBarView.frame = statusF.cellToolBarViewF!
        cellToolBarView.status = status
        
    }
    

}
